package com.ems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ems.entity.MenuEntity;
import com.ems.entity.RoleEntity;
import com.ems.entity.RoleMenuEntity;
import com.ems.mapper.RoleMapper;
import com.ems.mapper.RoleMenuMapper;
import com.ems.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ems.util.SnowflakeIdWorker;
import com.ems.util.StateEnum;
import com.ems.util.StateException;
import com.ems.vo.MenuVo;
import com.ems.vo.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2023-10-24
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements IRoleService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<RoleVo> queryList ( RoleVo roleVo ) {
        RoleEntity roleEntity = new RoleEntity ();
        BeanUtils.copyProperties ( roleVo, roleEntity );
        roleEntity.setPage ( ( roleEntity.getPage () - 1 ) * roleEntity.getLimit () );
        List<RoleEntity> roleEntities = roleMapper.queryList ( roleEntity );
        List<RoleVo> roleVos = new ArrayList<> ();
        for ( RoleEntity role : roleEntities ) {
            RoleVo vo = new RoleVo ();
            BeanUtils.copyProperties ( role, vo );
            roleVos.add ( vo );
        }
        return roleVos;
    }

    @Override
    public Integer queryCount ( RoleVo roleVo ) {
        RoleEntity roleEntity = new RoleEntity ();
        BeanUtils.copyProperties ( roleVo, roleEntity );
        Integer count = roleMapper.queryCount ( roleEntity );
        return count;
    }


    /**
     * 查询一个角色详情，且对应的菜单权限列表
     *
     * @param roleVo
     * @return
     */
    @Override
    public RoleVo querySingle ( RoleVo roleVo ) {
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<> ();
        if ( StringUtils.isNotBlank ( roleVo.getId () ) ) {
            queryWrapper.eq ( "id", roleVo.getId () );
        }
        RoleEntity roleEntity = baseMapper.selectOne ( queryWrapper );
        BeanUtils.copyProperties ( roleEntity, roleVo );
        // 该角色对应的所有二级权限路由
        RoleMenuEntity roleMenuEntity = new RoleMenuEntity ();
        roleMenuEntity.setRoleId ( roleVo.getId () );
        List<MenuEntity> menuEntities = roleMenuMapper.queryList ( roleMenuEntity );
        List<MenuVo> menuVos = new ArrayList<> ();
        for ( MenuEntity menuEntity : menuEntities ) {
            MenuVo menuVo = new MenuVo ();
            BeanUtils.copyProperties ( menuEntity, menuVo );
            menuVos.add ( menuVo );
        }
        roleVo.setMenuEntities ( menuVos );
        return roleVo;
    }


    /**
     * @param roleVo
     * @return
     */
    @Override
    public RoleVo saveSingle ( RoleVo roleVo ) {
        RoleEntity roleEntity = new RoleEntity ();
        BeanUtils.copyProperties ( roleVo, roleEntity );
        // 新增角色
        String id = SnowflakeIdWorker.getInstance ().nextStringId ();
        roleEntity.setId ( id );
        roleEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( roleEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return roleVo;
    }

    @Override
    public RoleVo updateSingle ( RoleVo roleVo ) {
        return null;
    }

    @Override
    public List<RoleVo> getRoleList ( RoleVo roleVo ) {
        List<RoleEntity> roleEntities = baseMapper.selectList ( new QueryWrapper<> () );
        List<RoleVo> roleVos = new ArrayList<> ();
        for ( RoleEntity roleEntity : roleEntities ) {
            RoleVo vo = new RoleVo ();
            BeanUtils.copyProperties ( roleEntity, vo );
            roleVos.add ( vo );
        }
        return roleVos;
    }
}
